﻿using Common;
using RentalDTO;
using RentalIService;
using RentalService.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace RentalService
{
    public class RoleService : IServiceRole
    {
        public List<RoleDTO> selectList()
        {
            using (var db = new RentalContext())
            {
                BaseService<Role> bs = new BaseService<Role>(db);
                Expression<Func<Role, bool>> where = u => true;

                var list = bs.GetLists(where).Select(u => new RoleDTO
                {
                    Id = u.Id,
                    Name = u.Name,
                    IsDel = u.IsDel

                }).ToList();

                return list;
            }
        }


        public List<RoleDTO> ShowRoleList(int offset, int limit, string sort, string order, string name, ref int rowCount)
        {
            using (var db = new RentalContext())
            {
                BaseService<Role> bs = new BaseService<Role>(db);
                rowCount = 0;
              
                var query = Common.PredicateExtensions.True<Role>();
                query = query.And<Role>(u => u.IsDel == 1);

                if (!String.IsNullOrWhiteSpace(name))
                {
                  
                    query = query.And<Role>(u => u.Name.Contains(name));
                }
                var orderBy = Common.PredicateExtensions.GetSortLambda<Role>(sort);

                bool orderType = order == "asc" ? true : false;
                var list = bs.GetPagedList(offset, limit, ref rowCount, query, orderBy, orderType).Select(m => new RoleDTO
                {
                    Id = m.Id,
                    Name = m.Name,
                
                }).ToList();
                return list;
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Role"></param>
        /// <returns></returns>
        public bool Add(string name)
        {
            using (var db = new RentalContext())
            {
                BaseService<Role> role = new BaseService<Role>(db);
                Role model = new Role()
                {
                    Name = name,
                    Id = Guid.NewGuid().ToString(),
                    IsDel = 1
                };

                return role.Add(model);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Del(string id)
        {
            using (var db = new RentalContext())
            {
                BaseService<Role> role = new BaseService<Role>(db);
                var mode = db.Role.Find(id);
                mode.IsDel = 0;
                return role.Delete(mode);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool up(string name, string id)
        {
            using (var db = new RentalContext())
            {
                BaseService<Role> role = new BaseService<Role>(db);
                var model = db.Role.Find(id);
                model.Name = name;
                return role.Update(model);
            }
        }

        public List<RoleDTO> SelectRoleID(string yor)
        {
            using (var db = new RentalContext())
            {
                BaseService<Role> bs = new BaseService<Role>(db);


                //var query = Common.PredicateExtensions.True<Role>();
                Expression<Func<Role, bool>> query = u => u.IsDel == 1;

                if (!String.IsNullOrWhiteSpace(yor))
                {

                    query = query.And(u => u.Id.Contains(yor));
                }

                var list = bs.GetLists(query).Select(m => new RoleDTO
                {
                    Id = m.Id,
                    Name = m.Name,

                }).ToList();
                return list;
            }
        }
    }
}
